﻿@model CustomerAddressListModel
@using Grand.Web.Models.Customer;
@{
    Layout = "_ColumnsTwo";

    //title
    Html.AddTitleParts(T("PageTitle.Account").Text);
}
@section left
{
    @await Component.InvokeAsync("CustomerNavigation", new { selectedTabId = CustomerNavigationEnum.Addresses })
}
<div class="page account-page address-list-page pl-lg-3 pt-lg-0 pt-3">
    <a class="js-offcanvas-trigger blog-filter filter-button btn btn-outline-info d-lg-none d-flex mb-3" data-offcanvas-trigger="leftSide" href="#leftSide"><span class="mdi mdi-tune"></span><span class="ml-3">@T("Account.Navigation")</span></a>
    <button type="button" class="btn btn-info btn-sm add-address-button float-sm-right" onclick="location.href='@Url.RouteUrl("CustomerAddressAdd")'">
        <i class="fa fa-plus" aria-hidden="true"></i> @T("Common.AddNew")
    </button>
    <h1 class="h2 generalTitle">@T("Account.CustomerAddresses")</h1>
    @if (Model.Addresses.Any())
    {
        <div class="col-12 px-0">
            <div class="row address-list">
                @for (int i = 0; i < Model.Addresses.Count; i++)
                {
                    var address = Model.Addresses[i];
                    <div class="col-md-6 col-12 mb-3">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">
                                    <h4><strong>@address.FirstName @address.LastName</strong></h4>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="address-item">
                                    <ul class="info mb-0">
                                        <li class="name">
                                            @address.FirstName @address.LastName
                                        </li>
                                        <li class="email">
                                            <label class="mb-0"><strong>@T("Address.Fields.Email"):</strong></label>
                                            @address.Email
                                        </li>
                                        @if (address.PhoneEnabled)
                                        {
                                            <li class="phone">
                                                <label class="mb-0"><strong>@T("Address.Fields.PhoneNumber"):</strong></label>
                                                @address.PhoneNumber
                                            </li>
                                        }
                                        @if (address.FaxEnabled)
                                        {
                                            <li class="fax">
                                                <label class="mb-0"><strong>@T("Address.Fields.FaxNumber"):</strong></label>
                                                @address.FaxNumber
                                            </li>
                                        }
                                        @if (address.CompanyEnabled && !String.IsNullOrEmpty(address.Company))
                                        {
                                            <li class="company">@address.Company</li>
                                        }
                                        @if (address.VatNumberEnabled && !String.IsNullOrEmpty(address.VatNumber))
                                        {
                                            <li class="vatnumber">@address.VatNumber</li>
                                        }
                                        @if (address.StreetAddressEnabled)
                                        {
                                            <li class="address1">
                                                @address.Address1
                                            </li>
                                        }
                                        @if (address.StreetAddress2Enabled && !String.IsNullOrEmpty(address.Address2))
                                        {
                                            <li class="address2">
                                                @address.Address2
                                            </li>
                                        }
                                        @if (address.CityEnabled || address.StateProvinceEnabled || address.ZipPostalCodeEnabled)
                                        {
                                            var cityStateZip = "";
                                            if (address.CityEnabled)
                                            {
                                                cityStateZip = address.City;
                                                if (address.StateProvinceEnabled || address.ZipPostalCodeEnabled)
                                                {
                                                    cityStateZip += ",";
                                                }
                                            }
                                            if (address.StateProvinceEnabled)
                                            {
                                                if (!String.IsNullOrEmpty(cityStateZip))
                                                {
                                                    cityStateZip += " ";
                                                }
                                                cityStateZip += address.StateProvinceName;
                                            }
                                            if (address.ZipPostalCodeEnabled)
                                            {
                                                if (!String.IsNullOrEmpty(cityStateZip))
                                                {
                                                    cityStateZip += " ";
                                                }
                                                cityStateZip += address.ZipPostalCode;
                                            }
                                            <li class="city-state-zip">
                                                @cityStateZip
                                            </li>
                                        }
                                        @if (address.CountryEnabled && !String.IsNullOrEmpty(address.CountryName))
                                        {
                                            <li class="country">
                                                @address.CountryName
                                            </li>
                                        }
                                        @if (!String.IsNullOrEmpty(address.FormattedCustomAddressAttributes))
                                        {
                                            <li class="custom-attributes-view">
                                                @Html.Raw(address.FormattedCustomAddressAttributes)
                                            </li>
                                        }
                                    </ul>
                                </div>
                            </div>
                            <div class="card-footer px-0 buttons text-xs-center btn-group btn-group-sm" role="group">
                                <input type="button" class="btn btn-secondary edit-address-button" onclick="location.href='@(Url.RouteUrl("CustomerAddressEdit", new { addressId = address.Id }))'" value='@T("Common.Edit")' />
                                <input type="button" class="btn btn-danger delete-address-button" onclick="deletecustomeraddress('@(address.Id)')" value="@T("Common.Delete")" />
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
        <script asp-location="Footer">
                function deletecustomeraddress(addressId) {
                    if (confirm('@T("Admin.Common.AreYouSure")')) {
                        var postData = {
                            addressId: addressId
                        };
                        addAntiForgeryToken(postData);

                        $.ajax({
                            cache: false,
                            type: 'POST',
                            url: '@Url.Action("AddressDelete", "Customer")',
                            data: postData,
                            dataType: 'json',
                            success: function (data) {
                                location.href = data.redirect;
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert('Failed to delete');
                            }
                        });
                    }
                }
        </script>
    }
    else
    {
        <div class="no-data">
            @T("Account.CustomerAddresses.NoAddresses")
        </div>
    }
</div>
